﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

using WHelper = PGMHelper.WilsonHelper;
using PGMHelper;


namespace HystereticBasic
{
    public partial class DetailForm : Form
    {
        /// <summary>
        /// 数据点数据
        /// </summary>
        private HyDataSet DATAS { set; get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DetailForm(HyDataSet datas)
        {
            InitializeComponent();
            //获得数据
            this.DATAS = datas;
            //Chart
            this.DetailChart.SetApperance(StringAlignment.Center, false);
            //添加标题
            this.DetailChart.AddTitles("", "序号", "相\n关\n参\n数");
            //科学技术法
            this.DetailChart.SetAxisFormat(ChartAxisType.AxisY, AxisFormat.Science, 1);
            this.DetailChart.SetAxisFormat(ChartAxisType.AxisX, AxisFormat.Normal, 0);
            //Tab外观
            this.DetailTabControl.SetApperance();
            //DetailDataView外观
            this.DetailDataView.SetApperance(DataGridViewSelectionMode.ColumnHeaderSelect, 40);
            //导入文件列表数据
            this.FileNameCheckedListBox.SetDataSource(this.DATAS.DataList, "FileName", "FileName");
        }

        /// <summary>
        /// 获得数据类型
        /// </summary>
        private DataType DataType
        {
            get
            {
                //能量
                if (this.EnergyRadioButton.Checked) return DataType.AccumulateEnergy;
                //粘滞系数
                else if (this.EnergyRatioRadioButton.Checked)
                    return DataType.ViscousDampingRatio;
                //割线刚度
                else if (this.SecantStiffRaditoButton.Checked)
                    return DataType.SecantStiffness;
                //刚度退化系数
                else if (this.DegraCoeRadioButton.Checked)
                    return DataType.SecantStiffnessRatio;
                //残余变形
                else if (this.UnloadDispRadio.Checked)
                    return DataType.AbsUnloadDisp;
                //残余变形系数
                else if (this.UnloadDispRatioRadio.Checked)
                    return DataType.UnloadDispRatio;
                //环最大承载力
                else if (this.CyclicMaxForceRadioButton.Checked)
                    return DataType.AbsCycleMaxForce;
                //环最大承载力系数
                else if (this.CyclicMaxForceRatioRadioButton.Checked)
                    return DataType.CycleMaxForceRatio;
                //最大位移（正值）
                else return DataType.AbsCycleMaxDisp;
            }
        }

        /// <summary>
        /// 选择参数类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DetailRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            //更新Chart
            this.SetChartGridView();
        }

        /// <summary>
        /// 文件选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileNameCheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //更新Chart
            this.SetChartGridView();
        }

        /// <summary>
        /// 绘图
        /// </summary>
        private void SetChartGridView()
        {
            //序列清空
            this.DetailChart.Series.Clear();
            //获得对象
            var dataList = this.FileNameCheckedListBox.GetObjects().ConvertAll(b => (BasicData)b);
            //绘图
            dataList.ForEach(data => 
            data.CycleDetailToChart(this.DetailChart, this.DataType, this.PosNegCheckbox.Checked, this.FilterCycleRadioButton.Checked));
            //修改图名
            this.DetailChart.SetTitle(TiltleType.Header, HystereticHelper.GetDescp(this.DataType));
            //修改表格
            this.DetailDataView.AddDatas(this.getData(dataList));
        }

        /// <summary>
        /// 获得绘图数据
        /// </summary>
        /// <param name="dataList"></param>
        /// <returns></returns>
        private GridViewDatas getData(List<BasicData> dataList)
        {
            //构造
            var GridData = new GridViewDatas(HystereticHelper.GetDescp(this.DataType));
            //遍历数据
            dataList.ForEach(data =>
            GridData.AddRange(data.GetData(new List<DataType> { DataType.CycleIndex, this.DataType },
                this.PosNegCheckbox.Checked,
                this.FilterCycleRadioButton.Checked)));
            return GridData;
        }

        /// <summary>
        /// 选择所有对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectAllButton_Click(object sender, EventArgs e)
        {
            this.FileNameCheckedListBox.CheckedAll();
            this.SetChartGridView();
        }

        /// <summary>
        /// 反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InverseSelectButton_Click(object sender, EventArgs e)
        {
            this.FileNameCheckedListBox.InverseChecked();
            this.SetChartGridView();
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutputButton_Click(object sender, EventArgs e)
        {
            this.DATAS.ToXLSX(this.PosNegCheckbox.Checked, this.FilterCycleRadioButton.Checked);
        }
    }
}
